/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of Descent Assistant.
 *
 * Descent Assistant is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Descent Assistant is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Descent Assistant.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.descenttool;

import org.amphiprion.descenttool.util.DeviceUtil;
import org.amphiprion.descenttool.util.PreferenceUtil;
import org.amphiprion.descenttool.util.PreferenceUtil.Preference;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author Amphiprion
 * 
 */
public class PreferencesActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preference);

		final LinearLayout llBase = (LinearLayout) findViewById(R.id.layoutBasic);
		final LinearLayout llCampaign = (LinearLayout) findViewById(R.id.layoutCampaign);
		final Button btRestoreXML = (Button) findViewById(R.id.btRestore);
		btRestoreXML.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Home.extractXMLs(true)) {
					Toast.makeText(PreferencesActivity.this, getResources().getText(R.string.extract_xml_ok), Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(PreferencesActivity.this, getResources().getText(R.string.extract_xml_ko), Toast.LENGTH_LONG).show();
				}
			}
		});
		final CheckBox chkVibration = (CheckBox) findViewById(R.id.chkVibration);
		chkVibration.setChecked(PreferenceUtil.getBoolean(Preference.DISABLE_VIBRATION, false));

		final Spinner cbClick = (Spinner) findViewById(R.id.cbClick);
		cbClick.setSelection(PreferenceUtil.getInt(Preference.UPDATE_CLICK_MODE, 0));

		final Spinner cbMode = (Spinner) findViewById(R.id.cbMode);
		cbMode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (cbMode.getSelectedItemPosition() == 0) {
					llBase.setVisibility(View.GONE);
					llCampaign.setVisibility(View.GONE);
				} else if (cbMode.getSelectedItemPosition() == 1) {
					llBase.setVisibility(View.VISIBLE);
					llCampaign.setVisibility(View.GONE);
				} else if (cbMode.getSelectedItemPosition() == 2) {
					llBase.setVisibility(View.GONE);
					llCampaign.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		cbMode.setSelection(PreferenceUtil.getInt(Preference.ENCOUNTER_MODE, 0));

		final Spinner cbNbPlayer = (Spinner) findViewById(R.id.cbNbPlayer);
		cbNbPlayer.setSelection(5 - PreferenceUtil.getInt(Preference.NB_PLAYER, 5));
		final Spinner cbLevelLieutenant = (Spinner) findViewById(R.id.cbLevelLieutenant);
		cbLevelLieutenant.setSelection(PreferenceUtil.getInt(Preference.LEVEL_LIEUTENANT, 0));
		final Spinner cbLevelHumanoid = (Spinner) findViewById(R.id.cbLevelHumanoid);
		cbLevelHumanoid.setSelection(PreferenceUtil.getInt(Preference.LEVEL_HUMANOID, 0));
		final Spinner cbLevelBeast = (Spinner) findViewById(R.id.cbLevelBeast);
		cbLevelBeast.setSelection(PreferenceUtil.getInt(Preference.LEVEL_BEAST, 0));
		final Spinner cbLevelEldritch = (Spinner) findViewById(R.id.cbLevelEldritch);
		cbLevelEldritch.setSelection(PreferenceUtil.getInt(Preference.LEVEL_ELDRITCH, 0));

		Button btOk = (Button) findViewById(R.id.btOk);
		btOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PreferenceUtil.putBoolean(Preference.DISABLE_VIBRATION, chkVibration.isChecked());
				DeviceUtil.disableVibration(chkVibration.isChecked());
				PreferenceUtil.putInt(Preference.UPDATE_CLICK_MODE, cbClick.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.ENCOUNTER_MODE, cbMode.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.NB_PLAYER, 5 - cbNbPlayer.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.LEVEL_LIEUTENANT, cbLevelLieutenant.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.LEVEL_HUMANOID, cbLevelHumanoid.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.LEVEL_BEAST, cbLevelBeast.getSelectedItemPosition());
				PreferenceUtil.putInt(Preference.LEVEL_ELDRITCH, cbLevelEldritch.getSelectedItemPosition());
				setResult(RESULT_OK);
				finish();
			}
		});

		Button btCancel = (Button) findViewById(R.id.btCancel);
		btCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
}
